package cc.itstone.subject.application.controller;


import cc.itstone.subject.domain.service.SubjectCategoryDomainService;
import com.alibaba.fastjson.JSON;
import cc.itstone.subject.application.DTO.SubjectCategoryDTO;
import cc.itstone.subject.application.DTO.SubjectInfoDTO;
import cc.itstone.subject.application.convert.SubjectCategoryConverter;
import cc.itstone.subject.common.entity.Result;
import cc.itstone.subject.domain.entity.SubjectCategoryBO;
import cc.itstone.subject.domain.service.SubjectInfoDomainService;
import cc.itstone.subject.infra.basic.entity.SubjectCategory;
import cc.itstone.subject.infra.basic.utlis.CopyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/subject")
public class SubjectController {

    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    /**
     * 新增题目分类
     *
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.add.Dto{}", JSON.toJSONString(subjectInfoDTO));
            }
            SubjectCategoryBO subjectCategoryBO = CopyUtil.copy(subjectInfoDTO, SubjectCategoryBO.class);
            subjectCategoryDomainService.add(subjectCategoryBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectController.add.error{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 查询岗位大类
     */
    @PostMapping("/queryPrimaryCategory")
    public Result queryPrimaryCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.queryPrimaryCategory.Dto{}", JSON.toJSONString(subjectCategoryDTO));
            }
            SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE.convertDtoToCategory(subjectCategoryDTO);
            List<SubjectCategory> subjectCategoryList = subjectCategoryDomainService.queryCategory(subjectCategory);
            List<SubjectCategoryDTO> subjectCategoryDTOS = SubjectCategoryConverter.INSTANCE.convertToCategoryDtoList(subjectCategoryList);
            return Result.ok(subjectCategoryDTOS);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.fail("查询失败");
        }
    }

    /**
     * 根据分类id查二级分类
     */
    @PostMapping("/queryCategoryByPrimary")
    public Result queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.queryCategoryByPrimary.Dto{}", JSON.toJSONString(subjectCategoryDTO));
            }

            SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE.convertDtoToCategory(subjectCategoryDTO);
            List<SubjectCategory> subjectCategoryList = subjectCategoryDomainService.queryCategory(subjectCategory);
            List<SubjectCategoryDTO> subjectCategoryDTOS = SubjectCategoryConverter.INSTANCE.convertToCategoryDtoList(subjectCategoryList);
            return Result.ok(subjectCategoryDTOS);
        }catch (Exception e){
            log.error("SubjectCategoryController.queryCategoryByPrimary.error:{}", e.getMessage(), e);
            return Result.fail("查询失败");
        }
    }

}
